package com.lanhai.zhonglai.third;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

import java.util.Arrays;
import java.util.List;

/**
 * 拆包机
 * 
 * @author zhengwei
 *
 */
public class CaiBao implements IStationProtocol {
	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[1] = 1; 
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[1] = 2; 
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		StationDataBean obj = new StationDataBean();
		obj.setLine((byte)1);
		obj.setCmdStr(Tools.byte2HexStr(buf));
		obj.setIp(ip);
		if (buf[1] == 4) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error);
			return Arrays.asList(obj);
		}
		if (buf[1] == 3  ) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave);
			return Arrays.asList(obj);
		}
		if (buf[1] == 2  ) {
			// 允许传输
			obj.setEventType(StationDataTypeEnum.AllowTrans);
			return Arrays.asList(obj);
		}
		if (buf[1] == 5 ) {
			// 允许传输
			obj.setEventType(StationDataTypeEnum.Receive);
			return Arrays.asList(obj);
		}
		StationMountBean mb = new StationMountBean();
		obj.setEventType(StationDataTypeEnum.Info);
	 
		int income = buf[5];
		mb.setInNum(income);
		if (income <=2) {
			mb.setAllowIn(true);
		}

		int outnum = buf[3];
		mb.setOutNum(outnum);
		if (outnum >= 2) {
			mb.setAllowOut(true);
			} 
		obj.setBean(mb);
		return Arrays.asList(obj);
	}

	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[1] = 0; 
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[1] =3;  
		return buf;
	}

	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[1] = 4; 
		return buf;
	}
}
